package com.athuahua.spzx.feign;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.http.protocol.RequestContent;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @Author: huahua
 * @Date: 2024/8/19 0019
 * @Time: 14:01
 * @Description: 用于传递oepnfegin远程调用的token ，需要在启动类中配合@EnableUserTokenFeignInterceptor注解使用，
 */
public class UserTokenOpenFeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //获取token
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader("token");
        //远程调用的时候可以传递token
        requestTemplate.header("token", token);


    }
}
